// pages/goods_detail/index.js
import { request } from "../../request/index.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 商品信息，这里对会提取需要的信息，而不是直接赋值
    goodsObj: {},
    // 底部工具栏数据
    btm_info:{
      left_info:[
        {
          id:1,
          icon:"/icons/kefu.png",
          value:"客服",
          url:""
        },
        {
          id:2,
          icon:"/icons/fenxiang.png",
          value:"分享",
          url:""
        },
        {
          id:2,
          icon:"/icons/cart.png",
          value:"购物车",
          url:"/pages/cart/index"
        }
      ],
      right_info:[
        {
          id:1,
          value:"加入购物车",
          handle:"handleCartAdd"
        },
        {
          id:2,
          value:"立即购买",
          handle:"handleBuy"
        }
      ] 
    },
    // 是否被收藏
    isCollect:false
  },
  GoodsInfo: {},

  onShow: function () {
    let pages=getCurrentPages();
    const {goods_id}=pages[pages.length-1].options
    this.getGoodsDetail(goods_id)
  },

  getGoodsDetail(goods_id){
    request({url:"/goods/detail",data:{goods_id}})
    .then(res=>{
      // res.data.message
      const goodsObj=res.data.message
      this.GoodsInfo = goodsObj;
      // 1、获取缓存中的商品收藏数组
      let collect=wx.getStorageSync("collect")||[];
      // 2、判断该商品是否在其中
      let isCollect=collect.some(v=>v.goods_id===this.GoodsInfo.goods_id)
      // 3、给bool值赋值
      this.setData({ 
        goodsObj: {
          goods_name: goodsObj.goods_name,
          goods_price: goodsObj.goods_price, 
          // iphone部分手机 不识别 webp图片格式 
          // 最好找到后台 让他进行修改 
          // 临时自己改 确保后台存在 1.webp => 1.jpg
          goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg'),
          pics: goodsObj.pics
        },
        isCollect
      })
    })
  },
  // 放大镜预览图片
  handlePrevewImage(e) {
    // 1 先构造要预览的图片数组 
    const urls = this.GoodsInfo.pics.map(v => v.pics_mid);
    // 2 接收传递过来的图片url
    const current = e.currentTarget.dataset.url;
    wx.previewImage({
      current,
      urls
    }); 

  },
  // 点击收藏图标
  handleCollect(){
    let isCollect=false;
    let collect=wx.getStorageSync("collect")||[];
    let index =collect.findIndex(v=> v.goods_id===this.GoodsInfo.goods_id)
    // console.log(index);
    if(index!==-1){
      collect.splice(index,1);
      isCollect=false;
      wx.showToast({
        title: '取消成功',
        icon:"success",
        mask:true
      })
    }
    else{
      collect.push(this.GoodsInfo)
      // console.log(this.GoodsInfo);
      isCollect=true;
      wx.showToast({
        title: '收藏成功',
        icon:"success",
        mask:true
      })
    }
    wx.setStorageSync('collect', collect)
    this.setData({
      isCollect
    })
  },
  // 点击加入购物车
  handleCartAdd(){
    let cart=wx.getStorageSync('cart')||[];
    let index=cart.findIndex(v=>v.goods_id===this.GoodsInfo.goods_id)
    if(index===-1){
      this.GoodsInfo.num = 1;
      this.GoodsInfo.checked = true;
      // console.log(this.GoodsInfo);
      cart.push(this.GoodsInfo);
    }else{
      cart[index].num++;
    }
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: '加入成功',
      icon:'success',
      mask:"true"
    })
  },
  // 点击立即购买
  handleBuy(){
    wx.showToast({
      title: '这个还没有完成哈哈哈哈!',
    })
  }


})